package com.leadbank.baselbf.outils

import java.math.BigDecimal

/**
 * ClassName: StringUtil
 * Function: String 工具类
 * Date: 2020-04-22 - 09:14
 * Author 杜时光
 * Version 0.1
 * Copyright (c) 2017, www.leadfund.com.cn All Rights Reserved.
 * 上海利得金融科技集团版权所有.
 */
object StringUtil {

    /**
     * 空串
     */
    @JvmStatic
    fun nvl(obj: Any?): String {
        return if (obj == null) {
            ""
        } else if ("null" == obj.toString()) {
            ""
        } else {
            obj.toString().trim()
        }
    }

    /**
     * 判空
     */
    @JvmStatic
    fun isNull(str: String?): Boolean {
        val flag: Boolean

        flag = if (str == null) {
            true
        } else {
            val s = str.trim { it <= ' ' }
            "" == s
        }
        return flag
    }

    /**
     * 判空
     */
    @JvmStatic
    fun isEmpty(str: String?): Boolean {
        val flag: Boolean

        if ("null" == str) {
            return true
        }
        flag = if (str == null) {
            true
        } else {
            val s = str.trim { it <= ' ' }
            "" == s
        }
        return flag
    }

    /**
     * 转换成int
     */
    @JvmStatic
    fun toInt(obj: Any?): Int {
        try {
            if (obj != null && nvl(obj) != "") {
                return if (nvl(obj) == "")
                    0
                else
                    BigDecimal(obj.toString())
                            .toInt()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return 0
    }

    /**
     * 含有默认值
     */
    @JvmStatic
    fun toInt(obj: Any?, default: Int): Int {
        try {
            if (obj != null && nvl(obj) != "") {
                return if (nvl(obj) == "")
                    default
                else
                    BigDecimal(obj.toString())
                            .toInt()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return default
    }

    /**
     * 转换成int
     */
    @JvmStatic
    fun toFloat(obj: Any?): Float {
        try {
            if (obj != null && nvl(obj) != "") {
                return if (nvl(obj) == "")
                    0f
                else
                    BigDecimal(obj.toString()).toFloat()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return 0f
    }

    /***
     * @Title: strTrim
     * @Description: 去掉首尾空格
     * @param @param str1
     * @param @return    设定文件
     * @return String    返回类型
     * @throws
     */
    @JvmStatic
    fun strTrim(str1: String): String {
        var str = ""
        if (!isNull(str1)) {
            str = str1.trim()
        }
        return str
    }

    /**
     * 判断是否是数字
     */
    @JvmStatic
    fun isNum(str: String): Boolean {
        return str.matches("^(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$".toRegex())
    }

    /**
     * 拼接字符串
     *
     * @param str
     * @return String
     */
    @JvmStatic
    fun group2String(vararg str: String?): String {
        val sb = StringBuilder()
        for (string in str) {
            if (null != string) sb.append(string)
        }
        return sb.toString()
    }

    /**
     * 转换成boolean
     */
    @JvmStatic
    fun toBoolean(obj: Any?, def: Boolean): Boolean {
        return if (obj != null && nvl(obj) != "") {
            when {
                nvl(obj) == "" -> def
                nvl(obj) != "true" -> false
                else -> java.lang.Boolean.parseBoolean(obj
                        .toString())
            }
        } else def
    }

    @JvmStatic
    fun isEmpty(str: CharSequence?): Boolean {
        return str == null || str.isEmpty()
    }

    @JvmStatic
    fun isBlank(str: String?): Boolean {
        return str == null || str.trim().isEmpty()
    }

    @JvmStatic
    fun isNotBlank(str: String?): Boolean {
        var isNotBlank = str == null || str.trim().isEmpty()
        return !isNotBlank
    }
    @JvmStatic
    fun isNoData(str: String?): Boolean {
        var isNotBlank = str == null || str.trim().isEmpty() || str == "--"

        return !isNotBlank
    }
    @JvmStatic
    fun getSex(sexType: String): String {
        if ("0".equals(sexType))
            return "男"
        return "女"
    }

    @JvmStatic
    fun isY(flag: String?): Boolean {
        return !(flag == null || flag == "N")
    }

    /**
     * 获取字符串后四位
     */
    fun getLastFourChar(bankCode: String): String {
        return bankCode.substring(bankCode.length - 5)
    }
}